package com.teamwings.modules.track.service.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.teamwings.modules.track.entity.OatAppTrack;
import com.teamwings.modules.track.mapper.OatAppTrackMapper;
import com.teamwings.modules.track.service.IOatAppTrackService;
import com.teamwings.util.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 表单设计服务实现类
 * </p>
 *
 * @author admin
 * @since 2022-01-19
 */
@Service
@Slf4j
public class OatAppTrackServiceImpl extends ServiceImpl<OatAppTrackMapper, OatAppTrack> implements IOatAppTrackService {

    @Override
    public List<OatAppTrack> findByByInstId(String instId) {
        return this.baseMapper.findByByInstId(instId);
    }

    @Override
    public void save(String instId, String userId) {
        OatAppTrack oatAppTrack;
        List<OatAppTrack> oatAppTracks = this.baseMapper.findByInstIdAndUserId(instId, userId,null);
        if (oatAppTracks.size() > 0) {
            oatAppTrack = oatAppTracks.get(0);
            if (oatAppTrack.getFState() == 0) {
                return;
            }
            oatAppTrack.setFState(0);
        } else {
            oatAppTrack = new OatAppTrack();
            oatAppTrack.setFId(UUIDGenerator.getUUID());
            oatAppTrack.setFInstId(instId);
            oatAppTrack.setFUserId(userId);
            oatAppTrack.setFState(0);
        }
        this.saveOrUpdate(oatAppTrack);
    }

    @Override
    public void updateStateByInstIdAndUserId(String instId, String userId, Integer state) {
        this.baseMapper.updateStateByInstIdAndUserId(instId, userId, state);
    }

    @Override
    public boolean isTrack(String instId, String userId) {
        return this.baseMapper.findByInstIdAndUserId(instId, userId,0).size() > 0 ? true : false;
    }
}
